package com.funmi.lizip.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.plugins.pagination.PageDTO;
import com.funmi.lizip.common.base.R;
import com.funmi.lizip.common.base.ResultCode;
import com.funmi.lizip.common.exception.ApplicationException;
import com.funmi.lizip.entity.Task;
import com.funmi.lizip.entity.TaskDelivery;
import com.funmi.lizip.entity.TaskItem;
import com.funmi.lizip.entity.dto.task.TaskAddRequest;
import com.funmi.lizip.entity.dto.task.TaskQueryRequest;
import com.funmi.lizip.entity.dto.task.TaskUpdateRequest;
import com.funmi.lizip.entity.dto.taskDelivery.TaskDeliveryAddRequest;
import com.funmi.lizip.entity.dto.taskDelivery.TaskDeliveryQueryRequest;
import com.funmi.lizip.entity.dto.taskDelivery.TaskDeliveryUpdateRequest;
import com.funmi.lizip.entity.vo.TaskDeliveryVo;
import com.funmi.lizip.entity.vo.TaskVo;
import com.funmi.lizip.service.TaskItemService;
import com.funmi.lizip.service.TaskService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author itheima
 */
@Slf4j
@RestController
@RequestMapping("task")
@Api(tags = "数据采集和核查")
public class TaskController {

    @Autowired
    private TaskService  taskService;

    @Autowired
    private TaskItemService taskItemService;

    @ApiOperation(value = "任务的新增")
    @PostMapping("")
    public R addTask(@RequestBody TaskAddRequest request){
        if(request==null){
            throw new ApplicationException(ResultCode.NOT_FOUND);
        }

        request.setCreateAt(LocalDateTime.now());
        request.setIsDelete(false);
        Task task = new Task();
        BeanUtils.copyProperties(request,task);
        boolean save = taskService.save(task);

        if (save) {
            return R.success().message("新增成功");
        } else {
            throw new ApplicationException("新增失败");
        }
    }


    @ApiOperation(value = "任务的修改")
    @PutMapping("/{id}")
    public R updateTask(@RequestBody(required = false) TaskUpdateRequest request){
        if(request==null||request.getId()==null){
            throw new ApplicationException(ResultCode.NOT_FOUND);
        }
        Task task = new Task();
        BeanUtils.copyProperties(request,task);

        boolean update = taskService.updateById(task);
        if (update) {
            return R.success().message("修改成功");
        } else {
            throw new ApplicationException("修改失败");
        }
    }

    @ApiOperation(value = "任务的删除")
    @DeleteMapping("/{id}")
    public R deleteTask(@PathVariable Integer id){
        if (id <= 0) {
            throw new ApplicationException(ResultCode.FILE_DELETE_ERROR);
        }
        boolean result = taskService.removeById(id);
        if (result) {
            return R.success().message("删除成功");
        } else {
            return R.fail().message("删除失败");
        }
    }

    @ApiOperation("根据任务输入进行检索")
    @GetMapping("")
    public R listPage(TaskQueryRequest request){
        long current = 1;
        long size = 5;
        Task taskQuery = new Task();
        if(request!=null){
            BeanUtils.copyProperties(request,taskQuery);
            current =request.getPage();
            size =request.getPageSize();
        }

        QueryWrapper<Task> qw = new QueryWrapper<>(taskQuery);
        qw.select().orderByDesc("create_at");
        Page<Task> taskPage = taskService.page(new Page<>(current, size), qw);

        PageDTO<TaskVo> taskVoPage = new PageDTO<>(taskPage.getCurrent(), taskPage.getSize(), taskPage.getTotal());

        List<TaskVo> taskVoList = taskPage.getRecords().stream().map(task -> {
            TaskVo taskVo = new TaskVo();
            BeanUtils.copyProperties(task, taskVo);
            return taskVo;
        }).collect(Collectors.toList());
        taskVoPage.setRecords(taskVoList);
        return R.success().data(taskVoPage);
    }


    @ApiOperation("根据ID获取任务详情")
    @GetMapping("/{id}")
    public R listById(TaskQueryRequest request){
        if(request==null ){
            throw new ApplicationException(ResultCode.NOT_FOUND);
        }
        TaskVo  taskVo = taskService.listById(request);
        return  R.success().data(taskVo);
    }


}
